import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @projectName: java-daily-questions
 * @package: PACKAGE_NAME
 * @className: SharedVariableProblem
 * @author: ZM
 * @description: 共享变量问题
 * @date: 2021/8/22 18:14
 * @version: 1.0
 */
public class SharedVariableProblem {

    static int num = 0;

    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(0);
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            num++;
            // 释放两个许可证
            semaphore.release(2);
        }, "A").start();

        new Thread(() -> {
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "获取 num = " + num);
        }, "B").start();

        new Thread(() -> {
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "获取 num = " + num);
        },"C").start();
    }
}
